package com.app.core.configuration;

import com.app.core.exception.DefaultExceptionTranslator;
import com.app.core.exception.ExceptionTranslator;
import com.app.core.mvc.repeat.RedisRepeatCommitCache;
import com.app.core.mvc.repeat.RepeatCommitCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;

/**
 * @author qiangt
 * @since 2023/6/14 17:12
 */
@Configuration
@Slf4j
public class CraftsmanAutoConfiguration {

    @PostConstruct
    public void init() {
        log.info("配置 CraftsmanAutoConfiguration");
    }

    @Bean
    @ConditionalOnMissingBean(ExceptionTranslator.class)
    public ExceptionTranslator exceptionTranslator() {
        return new DefaultExceptionTranslator();
    }

    //    @Bean
    //    @ConditionalOnMissingBean(DictTranslator.class)
    //    public DictTranslator dictTranslator(final DictRepository dictRepository, final DictItemRepository dictItemRepository) {
    //        return new MySQLDictTranslator(dictRepository, dictItemRepository);
    //    }

    @Bean
    @ConditionalOnMissingBean(RepeatCommitCache.class)
    public RepeatCommitCache repeatCommitCache(final StringRedisTemplate stringRedisTemplate) {
        return new RedisRepeatCommitCache(stringRedisTemplate);
    }

    //    @Bean
    //    @ConditionalOnMissingBean(ReplayAttacksCache.class)
    //    public ReplayAttacksCache replayAttacksCache(final StringRedisTemplate stringRedisTemplate) {
    //        return new RedisReplayAttacksCache(stringRedisTemplate);
    //    }
    //
    //    @Bean
    //    @ConditionalOnMissingBean(ResourceOwnerFinder.class)
    //    public ResourceOwnerFinder resourceOwnerFinder() {
    //        return new DefaultResourceOwnerFinder();
    //    }
    //
    //    @Bean
    //    @ConditionalOnProperty(prefix = "craftsman.token", name = "redis-token", havingValue = "true")
    //    @ConditionalOnClass(RedisOperations.class)
    //    public TokenService redisTokenService(final CraftsmanTokenProperties craftsmanTokenProperties, final StringRedisTemplate stringRedisTemplate) {
    //        return new RedisTokenService(craftsmanTokenProperties, stringRedisTemplate);
    //    }
    //
    //    @Bean
    //    @ConditionalOnMissingBean(MetaObjectHandler.class)
    //    public MetaObjectHandler metaObjectHandler(final ResourceOwnerFinder resourceOwnerFinder) {
    //        return new EntityMetaObjectHandler(resourceOwnerFinder);
    //    }
    //
    //    @Bean
    //    @ConditionalOnMissingBean(MethodValidationPostProcessor.class)
    //    public MethodValidationPostProcessor methodValidationPostProcessor() {
    //        return new MethodValidationPostProcessor();
    //    }
    //
    //    @Bean
    //    @ConditionalOnMissingBean(CommonEnvironment.class)
    //    public CommonEnvironment commonEnvironment(final CommonProperties commonProperties, final Environment environment) {
    //        return new DefaultCommonEnvironment(commonProperties, environment);
    //    }

}
